//自定义js 
//公共配置


$(document).ready(function () {
    $("#tabs").tabs({
        onSelect: function (title, index) {
            //console.log("select tabs ：title：", title, "; index:", index);
        }
    });
    InitLeftMenu(); // 初始化左侧
    tabClose();
    tabCloseEven(); // 绑定右键菜单事件
});
function InitLeftMenu() {
    $.ajax({
        type: "get",
        url: "/sys/menu/GetMenuTreeDS",
        dataType: "json",
        success: function (r) {
            InitMenu(r);
        }
    });
}
//初始化左侧
function InitMenu(menus) { 
    // 手風琴 + 樹模式 
    var menulist = ""; 
    $.each(menus,
        function(i, n) {
            menulist += '<div title="' + n.text + '" data-options="iconCls:\'' + n.iconCls + '\'" style="overflow:auto;padding:10px;">';
            menulist += '<ul class="easyui-tree">';
            if (n.children) {
                $.each(n.children,
                    function (j, o) {
                        menulist += createTreeMenu(o);
                    });
            }
            
            menulist += '</ul></div>';
    });
    var menuObj = $(menulist).appendTo("#menuList");
    $.parser.parse(menuObj);
    $("#menuList").accordion({ border: false, fit: true });
    
    /*
    // 树模式开始
    var menulist = '<div  ><ul class="easyui-tree" id="treeMenu">';

    $.each(menus,
        function (i, n) { 
            menulist += createTreeMenu(n); 
        });
    menulist += '</ul></div>';
    $("#menuList").empty();
   
    var menuObj = $(menulist).appendTo('#menuList'); 
    $.parser.parse('#menuList');
    */
    // 树模式结束
    $('#menuList li a').click(function () {
        var tabTitle = $(this).text();
        var url = $(this).attr("href");
        var icon = $(this).attr("icon");
        addTab(tabTitle, url, icon);
        //$('#menuList li div').removeClass("selected");
        //$(this).parent().addClass("selected");
        return false;
    });
    $('#menuList').on("mouseover", "li a", function () {
        $(this).parent().addClass("hover");
    });
    $('#menuList').on("mouseout", "li a", function () {
        $(this).parent().removeClass("hover");
    });  
} 
function createTreeMenu(node) {
   
    var html = '<li data-options="iconCls:\'' + node.iconCls + '\'">';
    if (node.children && node.children.length > 0) {
        var  nodedivTmp = '<ul>';
        for (var i = 0; i < node.children.length; i++) {
            nodedivTmp += createTreeMenu(node.children[i]);
        }
        nodedivTmp += '</ul>';
        html += '<span>' + node.text + '</span>' + nodedivTmp;
    } else {
        html += '<a target="mainFrame" icon="' + node.iconCls + '" href="' + node.url + '" ><span>' + node.text + '</span></a>';
    }
    return html + '</li> '; 
}

function addTab(subtitle, url, icon) { 
    if (!$('#tabs').tabs('exists', subtitle)) {
        $('#tabs').tabs('add', {
            title: subtitle,
            content: createFrame(url),
            closable: true,
            width: $('#mainPanle').width() - 10,
            height: $('#mainPanle').height() - 26,
            iconCls: icon
        });
    } else {
        $('#tabs').tabs('select', subtitle);
    }
    startPace();
}

function Tab(subtitle, url, icon) {
    $('#tabs').tabs('add', {
        title: subtitle,
        content: createFrame(url),
        closable: true,
        width: $('#mainPanle').width() - 10,
        height: $('#mainPanle').height() - 26,
        iconCls: icon
    });
    startPace();
}
function startPace() {
    $(".pace").removeClass("pace-inactive");
    Pace.start();
}
function createFrame(url) {
    var s = '<iframe name="mainFrame" scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe>';
    return s;
}

function tabClose() {
    /*双击关闭TAB选项卡*/
    $("#tabs").on("dblclick", ".tabs-inner", function () {
        $('#tabs').tabs('close', $(this).children("span").text());
    });
    $("#tabs").on("contextmenu", ".tabs-inner", function (e) {
        $('#mmTab').menu('show', {
            left: e.pageX,
            top: e.pageY
        });
        var subtitle = $(this).children("span").text();
        $('#mmTab').data("currtab", subtitle);
        return false;
    }); 
}
//绑定右键菜单事件
function tabCloseEven() {
    //关闭当前
    $('#mm-tabclose').click(function() {
        var currtab_title = $('#mm').data("currtab"); 
        $('#tabs').closeTab(currtab_title);
    });
    //全部关闭
    $('#mm-tabcloseall').click(function () {
        $('.tabs-inner span').each(function (i, n) { 
            var t = $(n).text();
            $('#tabs').closeTab(t);
        });
    });
    //关闭除当前之外的TAB
    $('#mm-tabcloseother').click(function () {
        var currtab_title = $('#mm').data("currtab");
        $('.tabs-inner span').each(function (i, n) { 
            var t = $(n).text();
            if (t != currtab_title)
                $('#tabs').closeTab(t);
        });
    });
    //关闭当前右侧的TAB
    $('#mm-tabcloseright').click(function () {
        var nextall = $('.tabs-selected').nextAll();
        if (nextall.length == 0) { 
            alert('后边没有啦~~');
            return false;
        }
        nextall.each(function (i, n) {
            var t = $('a:eq(0) span', $(n)).text();
            $('#tabs').closeTab(t);
        });
        return false;
    });
    //关闭当前左侧的TAB
    $('#mm-tabcloseleft').click(function () {
        var prevall = $('.tabs-selected').prevAll();
        if (prevall.length == 0) {
            alert('到头了，前边没有啦~~');
            return false;
        }
        prevall.each(function (i, n) { 
            var t = $('a:eq(0) span', $(n)).text();
            $('#tabs').closeTab(t);
        });
        return false;
    }); 
    //退出
    $("#mm-exit").click(function() {
        $('#mm').menu('hide');
    });
}
$.fn.closeTab = function (t) { 
    var $this = this;
    var tab = $this.tabs("getTab", t);
    if (tab) {
        var opt = tab.panel('options');
        if (opt.closable) {
            $this.tabs('close', t);
        }
    } 
}
 

//判断浏览器是否支持html5本地存储
function localStorageSupport() {
    return (('localStorage' in window) && window['localStorage'] !== null);
}

// 初始化主题 
function InitThemes() {
    $.ajax({
        url: '/home/Themes',
        method: 'post',
        dataType: "json",
        success: function (data) { 
            $("#divThemes").html("");
            var html = " <ul>";
            for (var i = 0; i < data.length; i++) {
                var labelandbackground = data[i].label.split("|");
                var background = labelandbackground.length > 1 ? labelandbackground[1] : "#1abc9c;";
                var label = labelandbackground[0];
                html += "<li><div class=\"" + data[i].value + "\" style=\"background: " + background + "\">" + label + "</div></li>";
            }
            html += "</ul>";
            $("#divThemes").html(html);
            if ($.cookie('sxtheme')) {
                var themeName = $("#admintheme").attr("_curtheme");
                // 添加勾选状态
                $(".themeItem ul li").removeClass('themeActive');
                $('.themeItem ul li .' + themeName.split('.css')[0]).parent().addClass('themeActive');
            }
        }
    });
}
$(function () {
    
    InitThemes();// 初始化主题 
    

    // 左侧导航分类选中样式
    //$(".panel-body .accordion-body>ul>li").on('click', function () {
    //    $(this).siblings().removeClass('super-accordion-selected');
    //    $(this).addClass('super-accordion-selected');

    //    //新增一个选项卡
    //    var tabUrl = $(this).data('url');
    //    var tabTitle = $(this).text();
    //    //tab是否存在
    //    if ($("#tt").tabs('exists', tabTitle)) {
    //        $("#tt").tabs('select', tabTitle);
    //    } else {
    //        var content = '<iframe scrolling="auto" frameborder="0"  src="' + tabUrl + '" style="width:100%;height:99%;"></iframe>';
    //        $('#tt').tabs('add', {
    //            title: tabTitle,
    //            //content: content,
    //            href: tabUrl,
    //            closable: true
    //        });
    //    }
    //});

    // 设置按钮的下拉菜单
    $('.super-setting-icon').on('click', function () {
        $('#mmUser').menu('show', {
            top: 50,
            left: document.body.scrollWidth - 160
        });
    });

    // 修改主题
    $('#themeSetting').on('click', function () {
        var themeWin = $('#win').dialog({
            width: 500,
            height: 260,
            modal: true,
            title: '主题设置',
            buttons: [{
                text: '保存',
                id: 'btn-sure',
                handler: function () {
                    themeWin.panel('close');
                    // css
                    var newTheme = $(".themeItem ul li.themeActive>div").attr('class');
                    $.cookie('sxtheme', newTheme); 
                    sx.changeTheme(newTheme);
                }
            }, {
                text: '关闭',
                handler: function () {
                    themeWin.panel('close');
                }
            }],
            onOpen: function () {
                $(".themeItem").show();
            }
        });
    });

    // 勾选主题
    $(".themeItem").on('click',"li", function () {
        $(".themeItem ul li").removeClass('themeActive');
        $(this).addClass('themeActive');
    });

    // 退出系统
    $("#loginOut").on('click', function () {
        $.messager.confirm('系统提示', '您确定要退出本次登录吗?', function (r) {
            if (r) {
                location.href = '/Login/out';
            }
        });
    });
}); 